package com.sysadmin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.common.base.BaseAction;
import com.common.pager.PageHelper;
import com.common.pager.PagerVO;
import com.common.util.StringUtil;
import com.sysadmin.service.UserService;
import com.sysadmin.vo.RolesVO;
import com.sysadmin.vo.UsersVO;

public class UserAction extends BaseAction {
	private UserService service = new UserService();
	
	private List<UsersVO> list = new ArrayList<UsersVO>();
	private UsersVO vo = new UsersVO();
	private PagerVO pagerVO = null;
	private String id="";
	String username = "";
	List<RolesVO> roleList = new ArrayList<RolesVO>();

	private String ids = null;
	public String find(){
		log.info("==========find============");
		PageHelper pager = new PageHelper(this.getRequest());
		
		list = service.find(vo,pager);
		roleList = service.findRole4Dict();
		List<RolesVO> roleList1 = service.findRole();
		pagerVO = pager.paginate();
		this.getRequest().setAttribute("roleList1", roleList1);
		return topager("/pages/sys/user/list.jsp");
		
	}
	
	public String edit(){
		log.info("==========edit============");
		if(!StringUtil.isBlank(id)){
			vo = service.get(id);
		}
		
		roleList = service.findRole();
		return topager("/pages/sys/user/edit.jsp");
		
	}
	
	public String save(){
		log.info("==========save============");
		String isSave = service.save(vo);
		if("0".equals(isSave)){
			roleList = service.findRole();
		}
		this.getRequest().setAttribute("saveFlag", isSave);
		return topager("/pages/sys/user/edit.jsp");
	}
	public void delete(){
		log.info("==========delete============"+ids);
		service.delete(ids);
		
	}
	public String isApply(){
		
		log.info("==========isApply====id===="+id+"===username====="+username);
		String isApply = service.isApply(username,id);
		log.info("==========isApply============"+isApply);
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.getWriter().write(isApply);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public String editPass(){
		
		return topager("/pages/sys/user/edit_pass.jsp");
	}
	public String updatPass(){
	
		String oldpassword = this.getRequest().getParameter("oldpassword");
		String password = this.getRequest().getParameter("password");
		UsersVO userVO = (UsersVO) this.getRequest().getSession().getAttribute("users");
		
		if(!StringUtil.isBlank(oldpassword)&&oldpassword.equals(userVO.getPassword())){
			service.updatPass(userVO.getId(), password);
			userVO.setPassword(password);
			this.getRequest().setAttribute("saveFlag", "1");
		}else{
			this.getRequest().setAttribute("saveFlag", "0");
		}
			
		
		return topager("/pages/sys/user/edit_pass.jsp");
	}
	
	public List<UsersVO> getList() {
		return list;
	}
	public void setList(List<UsersVO> list) {
		this.list = list;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public UsersVO getVo() {
		return vo;
	}
	public void setVo(UsersVO vo) {
		this.vo = vo;
	}

	public List<RolesVO> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<RolesVO> roleList) {
		this.roleList = roleList;
	}



	public PagerVO getPagerVO() {
		return pagerVO;
	}

	public void setPagerVO(PagerVO pagerVO) {
		this.pagerVO = pagerVO;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	
}
